home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / amitcp2_x_gcc.lha / autoinitd.c < prev    next >
C/C++ Source or Header  |  1994-01-12  |  5KB  |  183 lines

  1. char RCS_ID_AUTOINITD_C[] = "$Id: autoinitd.c,v 1.2 1994/01/12 18:40:51 jasegler Exp jasegler $";
  2. /*
  3.  * SAS C Autoinitialization Functions for Daemons
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Mon May 24 19:30:06 1993 ppessi
  12.  * Last modified: Mon Jul 19 14:24:30 1993 jraja
  13.  */
  14.  
  15. /****** inetdlib.doc/autoinitd **********************************************
  16.  
  17.  *   NAME
  18.  *      autoinitd - SAS C Autoinitialization Functions for Daemons
  19.  *
  20.  *   SYNOPSIS
  21.  *      void _STIopenSockets(void);
  22.  *      void _STDcloseSockets(void);
  23.  *      long server_socket;
  24.  *
  25.  *   DESCRIPTION
  26.  *      These are SASC autoinitialization functions for internet daemons
  27.  *      started by inetd, Internet super-server. Upon startup, the server
  28.  *      socket is obtained with ObtainSocket() library call. If successful,
  29.  *      the socket id is stored to the global variable server_socket. If the
  30.  *      socket is not obtainable, the server_socket contains value -1.
  31.  *      If the server_socket is not valid, the server may try to accept() a
  32.  *      new connection and act as a stand-alone server.
  33.  *
  34.  *   RESULT
  35.  *      server_socket - positive socket id for success or -1 for failure.
  36.  *
  37.  *   NOTES
  38.  *      _STIopenSockets() also checks that the system version is at
  39.  *      least 37. It puts up a requester if the bsdsocket.library
  40.  *      is not found or is of wrong version.
  41.  *
  42.  *      The autoinitialization and autotermination functions are
  43.  *      features specific to the SAS C6. However, these functions
  44.  *      can be used with other (ANSI) C compilers, too. Example
  45.  *      follows:
  46.  *
  47.  *      \* at start of main() *\
  48.  *
  49.  *      atexit(_STDcloseSockets);
  50.  *      _STDopenSockets();
  51.  *
  52.  *   AUTHOR
  53.  *      Jarno Rajahalme, Pekka Pessi,
  54.  *      the AmiTCP/IP Group <amitcp-group@hut.fi>,
  55.  *      Helsinki University of Technology, Finland.
  56.  *
  57.  *   SEE ALSO
  58.  *      serveraccept(), netutil/inetd
  59.  *
  60.  *****************************************************************************
  61.  *
  62.  */
  63.  
  64. #include <exec/types.h>
  65. #include <exec/libraries.h>
  66. #include <dos/dosextens.h>
  67.  
  68. #include <intuition/intuition.h>
  69.  
  70. #ifdef __GNUC__
  71. #include <clib/socket_protos.h>
  72. #include <clib/exec_protos.h>
  73. #include <clib/intuition_protos.h>
  74. #endif
  75.  
  76. #ifdef _DCC
  77. #include <clib/socket_protos.h>
  78. #include <clib/exec_protos.h>
  79. #include <clib/intuition_protos.h>
  80. #endif
  81.  
  82. #ifdef __SASC__
  83. #include <proto/socket.h>
  84. #include <proto/exec.h>
  85. #include <proto/intuition.h>
  86. #endif
  87.  
  88. #include <stdlib.h>
  89.  
  90. #include <inetd.h>
  91.  
  92. struct Library *SocketBase = 0L;
  93.  
  94. int errno = 0;
  95.  
  96. long server_socket = -1;
  97.  
  98. #ifndef SOCKETNAME
  99. #define SOCKETNAME "bsdsocket.library"
  100. #endif
  101.  
  102. #define SOCKETVERSION 2        /* minimum bsdsocket version to use */
  103.  
  104. #ifdef __SASC__
  105. extern STRPTR _ProgramName;    /* SAS startup module defines this :-) */
  106. #endif
  107.  
  108. #ifdef __GNUC__
  109. char _ProgramName[] = "GNU C Program";
  110. #endif
  111.  
  112. #ifdef _DCC
  113. char _ProgramName[] = "DCC Program";
  114. #endif
  115.  
  116. void
  117. _STIopenSockets (void)
  118. {
  119.   struct Process *me = (struct Process *) FindTask (0L);
  120.   struct DaemonMessage *dm = (struct DaemonMessage *) me->pr_ExitData;
  121.   static short done = 0;
  122.   struct Library *IntuitionBase;
  123.   STRPTR errorStr;
  124.  
  125.   if (done++)            /* try only once (SAS/C 6.2 liked to call this twice) */
  126.     return;
  127.  
  128.   /*
  129.    * Check OS version
  130.    */
  131.   if ((*(struct Library **) 4)->lib_Version < 37)
  132.     exit (20);
  133.  
  134.   /*
  135.    * Open bsdsocket.library
  136.    */
  137.   if ((SocketBase = OpenLibrary (SOCKETNAME, SOCKETVERSION)) != NULL)
  138.     {
  139.       /*
  140.          * Succesfull. Now tell bsdsocket.library the address of our errno
  141.        */
  142.       SetErrnoPtr (&errno, sizeof (errno));
  143.  
  144.       if (dm && server_socket == -1)
  145.     {
  146.       server_socket = ObtainSocket (dm->dm_Id, dm->dm_Family, dm->dm_Type, 0);
  147.       if (server_socket == -1)
  148.         exit (DERR_OBTAIN);
  149.     }
  150.       return;
  151.     }
  152.   else
  153.     errorStr = "AmiTCP/IP version 2 or later must be started first.";
  154.  
  155.   IntuitionBase = OpenLibrary ("intuition.library", 36);
  156.  
  157.   if (IntuitionBase != NULL)
  158.     {
  159.       struct EasyStruct libraryES;
  160.  
  161.       libraryES.es_StructSize = sizeof (libraryES);
  162.       libraryES.es_Flags = 0;
  163.       libraryES.es_Title = _ProgramName;
  164.       libraryES.es_TextFormat = errorStr;
  165.       libraryES.es_GadgetFormat = "Exit %s";
  166.  
  167.       EasyRequestArgs (NULL, &libraryES, NULL, (APTR) & _ProgramName);
  168.  
  169.       CloseLibrary (IntuitionBase);
  170.     }
  171.   exit (DERR_LIB);
  172. }
  173.  
  174. void
  175. _STDcloseSockets (void)
  176. {
  177.   if (SocketBase)
  178.     {
  179.       CloseLibrary (SocketBase);
  180.       SocketBase = NULL;
  181.     }
  182. }
  183.